package com.ys.controller.api.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.config.MenuId;
import com.ys.entity.business.TbBaseInfo;
import com.ys.entity.sys.YsCol;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.util.admin.ConditionUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import com.ys.entity.business.TbBaseInfo;
import com.ys.service.business.TbBaseInfoService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/inter/tbBaseInfo")
public class ApiInterTbBaseInfoController {
	private static final Logger logger = LoggerFactory.getLogger(ApiInterTbBaseInfoController.class);

//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);

	@Autowired
	private CommonApiService<TbBaseInfo> commonApiService;
	@Autowired
	private TbBaseInfoService tbBaseInfoService;


	
//	@RequiresPermissions("tpFbaPlanOrder:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TbBaseInfo cond){
		cond.setOrderby("A.createTime DESC")
		.setVType("jylx")
		.addLeftJoin("TCW_ManageTransact B ON B.Bid=A.ManageBid")
		.addLeftJoin("TCW_FinancialTransactionType C ON C.Bid=A.FinanceBid")
		.addField("B.IntegrityType manageintegrityType,C.IntegrityType financeintegrityType");
		
		return commonApiService.searchLay(cond);
	}


	@RequestMapping("/searchByBgs")
	@ResponseBody
	public LayuiTableVoJA searchByBgs(@ModelAttribute TbBaseInfo cond){

		cond.setVType("bgs");
		return commonApiService.searchLay(cond);
	}


	/*
	 * 彻底删除
	 */
	@PostMapping("/deleteTrue")
	@ResponseBody
	public R deleteTrue(HttpServletRequest request, HttpServletResponse response,
			String guid) {
		return tbBaseInfoService.deleteTrue(guid);
	}


	/**
	 * 交易类型 - 弹出框
	 */
	@RequestMapping("/searchForModalByJylx")
	@ResponseBody
	public LayuiTableVoJA searchForModalByJylx(@ModelAttribute TbBaseInfo cond){
		cond.setVType("jylx");

		return commonApiService.searchLay(cond);
	}

	/*
	 * 新增
	 */
	@RequestMapping("/add")
	@ResponseBody
	@RequiresPermissions("tbBaseInfo:add")
	public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params, String subData
	) {
		try {
			logger.info("新增传参：" + params);
			//修改
			return tbBaseInfoService.add(params);
		} catch (Exception e) {
			logger.error("新增出错：", e.getMessage(), e);
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	/**
	 * 修改
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params,String subData
	) {
		try {
			logger.info("修改传参：" + params);
			//修改
			return tbBaseInfoService.edit(params);
		} catch (Exception e) {
			logger.error("修改出错：", e.getMessage(), e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	/**
	 * 导出
	 */
	@RequestMapping("/export")
	@ResponseBody
	public R export(@ModelAttribute TbBaseInfo cond, String conditions){
		try {
			cond.setPage(0).setSize(999999)
					.where("A.VType='jylx'");
			cond = setSeachSql(cond);
			//获取菜单字段数据
			List<YsCol> listField = new YsCol()
					.setMenuId(MenuId.TB_BaseInfo)
					.setFrontShow(1)
					.setOrderby("A.SortNo asc")
					.queryList();
			//获取数据
			List<Map<String, Object>> listData = cond.queryMap();

			//处理数据
			ExportExcelUtils<TbBaseInfo> util = new ExportExcelUtils<>(new TbBaseInfo());
			String toFilePath = util.exportExcel(listData, listField, "业务交易类型");
			UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
			return R.ok(dto.getUrl());
		}catch (Exception e){
			logger.error("导出出错：",e.getMessage(),e);
			return R.error("导出失败，请稍后重试！");
		}
	}
	/**
	 * 设置查询的cond的sql设置
	 * @param cond
	 * @return
	 */
	public TbBaseInfo setSeachSql(TbBaseInfo cond){


		cond = commonApiService.dataAuth(MenuId.TB_BaseInfo, cond);
		return  cond;
	}
}
